﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NamTrungBS.DataLayer
{
    class SanPhamDao
    {
        private static NamTrungDBDataContext db;


        /// <summary>
        /// lay danh sach san pham trong ban san pham bởi số lượng
        /// </summary>
        /// <returns>tra ve list san pham kieu List<T></returns>
        public static List<SanPham> GetAllProductByCount(int soluong)
        {
            try
            {
                db = new NamTrungDBDataContext();
                List<SanPham> listSanPham = db.SanPhams.Where(sp => sp.SLTon <=soluong).ToList();
                return listSanPham;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// lay danh sach san pham trong ban san pham
        /// </summary>
        /// <returns>tra ve list san pham kieu List<T></returns>
        public static List<String> GetAllProduct()
        {
            try
            {
                db = new NamTrungDBDataContext();
                var listSanPham = db.SanPhams.Select(sp=>sp.TenSP).ToList();
                return listSanPham;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static List<SanPham> GetAllProduct1()
        {
            try
            {
                return db.SanPhams.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Lay danh sach san pham boi cap khach hang
        /// </summary>
        /// <param name="capkh"></param>
        /// <returns></returns>
        public static IQueryable GetAllSanPhamByDG(int capkh)
        {
            db = new NamTrungDBDataContext();
            IQueryable listSanPham=null;
            if (capkh == 1)
            {
                listSanPham = from sp in db.SanPhams
                               select new { sp.MaSP, sp.TenSP, sp.DVT, sp.SLTon, sp.DonGiaGoc, sp.DonGia1 };
            }
            else if (capkh == 2)
            {
                listSanPham = from sp in db.SanPhams
                              select new { sp.MaSP, sp.TenSP, sp.DVT, sp.SLTon, sp.DonGiaGoc, sp.DonGia2 };
            }
            else if (capkh == 3)
            {
                listSanPham = from sp in db.SanPhams
                              select new { sp.MaSP, sp.TenSP, sp.DVT, sp.SLTon, sp.DonGiaGoc, sp.DonGia3 };
            }
             
            return listSanPham;
        }



        public static IQueryable GetSanPhamByName(string name, int capKhachhang)
        {
            db = new NamTrungDBDataContext();
            IQueryable listSanPham = null;
            try
            {
                if (capKhachhang == 1)
                {
                    listSanPham = from sp in db.SanPhams
                                  where sp.TenSP.Contains(name)
                                  select new { sp.MaSP, sp.TenSP, sp.DVT, sp.SLTon, sp.DonGiaGoc, sp.DonGia1 };
                }
                else if (capKhachhang == 2)
                {
                    listSanPham = from sp in db.SanPhams
                                  where sp.TenSP.Contains(name)
                                  select new { sp.MaSP, sp.TenSP, sp.DVT, sp.SLTon, sp.DonGiaGoc, sp.DonGia2 };
                }
                else if (capKhachhang == 3)
                {
                    listSanPham = from sp in db.SanPhams
                                  where sp.TenSP.Contains(name)
                                  select new { sp.MaSP, sp.TenSP, sp.DVT, sp.SLTon, sp.DonGiaGoc, sp.DonGia3 };
                }
                return listSanPham;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Get one sanpham from sanphm table
        /// </summary>
        /// <param name="maSanpham"></param>
        /// <returns></returns>
        public static Object GetProductByID(int maSanpham)
        {
            try 
            {
                db = new NamTrungDBDataContext();
                var sanpham = db.SanPhams.Where(sp => sp.MaSP == maSanpham).FirstOrDefault();
                return sanpham;             
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static bool  NewSanPham(string n, string dvt, float sld, float slt, float dgg, float dg1, float dg2, float dg3)
        {
            try
            {
                db = new NamTrungDBDataContext();
                SanPham sp = new SanPham();
                sp.TenSP = n;
                sp.DVT = dvt;
                sp.SLDau =sld;
                sp.SLTon = slt;
                sp.DonGiaGoc = dgg;
                sp.DonGia1 = dg1;
                sp.DonGia2 = dg2;
                sp.DonGia3 = dg3;
                db.SanPhams.InsertOnSubmit(sp);
                db.SubmitChanges();
                return true;

            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
        }
            
    }
}
